<?php
class Mstation extends CI_Model {

    private $_table = 'station';
    public function __construct() {
        parent::__construct();
    }
    
    /**
     * Get the list of stations
     */
    public function listAllStations($record=null, $start=null, $order=null) {
        if ($record != null && $start != null) {
            $this->db->limit($record, $start);
        }
        if ($order != null) {
            $this->db->order_by($order);
        }
        $query = $this->db->get($this->_table);        
        return $query->result_array();
    }
    
    public function insertStation($data) {
        $this->db->insert($this->_table, $data);
        return $this;
    }
    
    public function updateStation($data_update, $id) {
        $this->db->where('id', $id);
        $this->db->update($this->_table, $data_update);
        return $this;
    }
    
    public function deleteStation($id) {
        $this->db->where('id', $id)->delete($this->_table);
        return $this;
    }
    
    public function getStationById($id) {
        $this->db->where('id', $id);
        return $this->db->get($this->_table)->row_array();
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function listStationByLineId($id) {
        $this->db->where('line_id', $id);
        return $this->db->get($this->_table)->result_array();
    }
    
    public function setStationInter($line_id, $id) {
        $this->db->where('id', $id);
        $this->db->update($this->_table, array('line_id' => $line_id));
        return $this;
    }
        
    
    public function deleteInter($id) {
        $this->db->where('id', $id);
        $this->db->update($this->_table, array('line_id' => 0));
    }
    
    public function deletePositionByLineId($position, $line_id) {
        $this->db->where('line_id', $line_id)->where('position_station', $position);
        $this->db->update($this->_table, array('position_station' => ''));
        return $this;
    }

}